#define  _CRT_SECURE_NO_WARNINGS 1
#include "List.h"


LTNode* LTBuyNode(LTDataType x) {
	LTNode* newnode= (LTNode*)malloc(sizeof(LTNode));
	if (newnode == NULL)
	{
		perror("malloc");
		return 1;
	}
	newnode->data =x;
	newnode->next = newnode->prev = newnode;
	return newnode;
}

LTNode* LTInit(){
	LTNode* phead = LTBuyNode(-1);
	return phead;
}

void LTPushBack(LTNode* phead, LTDataType x) {
	assert(phead);
	LTNode* newnode = LTBuyNode(x);
	newnode->prev = phead->prev;
	newnode->next = phead;
	phead->prev->next = newnode;
	phead->prev = newnode;
}

void LTPrint(LTNode* phead) {
	assert(phead);
	LTNode* pcur = phead->next;
	while (pcur != phead)
	{
		printf("%d->", pcur->data);
		pcur = pcur->next;
	}
	printf("\n");
}

void LTPushFront(LTNode* phead, LTDataType x) {
	assert(phead);
	LTNode* newnode = LTBuyNode(x);
	newnode->prev = phead;
	newnode->next = phead->next;
	phead->next->prev = newnode;
	phead->next = newnode;
}



void LTPopBack(LTNode* phead) {
	assert(phead);
	assert(phead->next != NULL);
	LTNode* ptail = phead->prev;
	ptail->prev->next = phead;
	phead->prev = ptail->prev;
	free(ptail);
	ptail = NULL;
}


void LTPopFront(LTNode* phead) {
	assert(phead);
	assert(phead->next != NULL);
	LTNode* pcur = phead->next;
	phead->next = pcur->next;
	pcur->next->prev = phead;
	free(pcur);
	pcur = NULL;
}

LTNode* LTFinde(LTNode* phead,LTDataType x) {
	assert(phead);
	LTNode* pcur = phead->next;
	while (pcur != phead)
	{
		if (pcur->data == x)
		return pcur;
		pcur = pcur->next;
	}
	return NULL;
}


void LTInsert(LTNode* pos,LTDataType x) {
	assert(pos);
	LTNode* newnode = LTBuyNode(x);
	newnode->prev = pos;
	newnode->next = pos->next;
	pos->next->prev = newnode;
	pos->next = newnode;
	
}


void LTErase(LTNode* pos) {
	assert(pos);
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
	pos = NULL;
}

void LTDesTroy(LTNode* phead) {
	assert(phead);
	LTNode* pcur = phead->next;
	while (pcur != phead)
	{
		LTNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
}